import os
import cv2

result_dir = 'E:/datasets/person_reid_train/'
person_dir = 'E:/datasets/person/'
person = os.listdir(person_dir)
person_imgs = []
for j in person:
    person_imgs.append(j)

fgbg = cv2.createBackgroundSubtractorKNN()

size = len(person_imgs)

for i in range(size):
    name = person_imgs[i]

    person_img = cv2.imread(person_dir + person_imgs[i])
    result = fgbg.apply(person_img)
    cv2.imwrite(result_dir + name, result)